Lab4


4531206721_4531207321  นาย เฉลิมพงศ์ สัตยาวิบูล และ นาย ชัยชนะ นิลวัชรารัง (6/8/2545 (11:32:01))
(SM=1, CM=12, ST=21, KY=3, TR=12:41)

MiniQuiz + TestScript
Mini-Quiz : Q1=4,Q2=3,Q3=2,Q4=1,Q5=4  (5.0 คะแนน)

JLab>javac Lab4.java
JLab>
JLab>java Selftest
6 ส.ค. 2545 11:31:51 java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.

>>JLab:<POINT>10.00</POINT>

JLab>

ได้ 15 คะแนน
Source Code
import jlab.JLabIO;

public class Lab4 {

  public static void main(final String[] args) {
    double a = JLabIO.readDouble("Enter angle (in degree) : ");
    double x = a / 180.0 * Math.PI;

    System.out.println("Math.sin(" + a + ") = " + Math.sin(x));
    System.out.println("Lab4.sin(" + a + ") = " + sin(x));
    System.exit(0);
  }

  static double sin(double x) {
    double sine;
    
    // given x (in radian)
    // compute the trigonometric sine of x using the formula
    // sin(x) = x - x^3/3! + x^5/5! - x^7/7! +...+ (-1)^n * x^(2n-1)/(2n-1)! + ...
    // store the result in variable "sine"

    // add your code here
    sine = x;
    double term = x;
    int n = 0;
    do {
      term = (-1) * term * x * x / ((n + 3) * (n + 2));
      sine += term;
      n += 2;
      }while(Math.abs(term)>1E-10);

    return sine;
  }
}